package com.example.usercenter.utils;

import com.example.usercenter.pojo.Captcha;
import com.example.usercenter.pojo.RegisterResult;
import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterUtil {

    /**
     * 返回响应码
     * @param phone
     * @param password
     * @param captcha
     * @return
     * @throws Exception
     */
    public static int register(String phone, String password, String captcha) throws Exception {

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\r\n  " +
                "\"captcha\": "+captcha+",\r\n  \"" +
                "password\": \""+password+"\",\r\n  " +
                "\"phone\": \""+phone+"\"\r\n}\r\n\r\n");

        Request request = new Request.Builder()
                .url(BaseUrl.baseUrl+"/dev-api/register")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();
        String responseData = response.body().string();
        Gson gson = new Gson();
        RegisterResult registerResult = gson.fromJson(responseData, RegisterResult.class);
        System.out.println("registerResult.getCode():"+registerResult.getCode());
        return registerResult.getCode();
    }
}
